package com.itrip.search.service;

import com.itrip.search.vo.HotelParam;
import com.itrip.search.vo.ItripHotelVO;
import com.itrip.utils.PageBean;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
@Service
public class SearchService {
    public List<ItripHotelVO> searchItripHotelListByHotCity(int cityId, int count) throws Exception{
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        query.addFilterQuery("cityId:"+cityId);
        query.setStart(0);
        query.setRows(count);
        List<ItripHotelVO> list = client.query(query).getBeans(ItripHotelVO.class);
        return list;
    }

    public void searchItripHotelPage(HotelParam hotelParam,PageBean pageBean) throws IOException, SolrServerException {
        String url = "http://localhost:9080/solr/hotel";
        HttpSolrClient client = new HttpSolrClient(url);
        client.setParser(new XMLResponseParser());
        client.setConnectionTimeout(500);
        SolrQuery query = new SolrQuery();
        query.setQuery("*:*");
        if(hotelParam.getDestination() != null && hotelParam.getDestination() != ""){
            query.addFilterQuery("destination:"+hotelParam.getDestination());
        }
        if(hotelParam.getKeywords() != null && hotelParam.getKeywords() != ""){
            query.addFilterQuery("keyword:"+hotelParam.getKeywords());
        }
        if(hotelParam.getTradeAreaIds() != null && hotelParam.getTradeAreaIds() != ""){
            String [] arr = hotelParam.getTradeAreaIds().split(",");
            StringBuffer tradeAreaIds = new StringBuffer();
           for(int i =0;i<arr.length;i++){
               if(i == 0){
                   tradeAreaIds.append("tradingAreaIds:*,"+arr[i]+",*");
               }else{
                   tradeAreaIds.append(" OR tradingAreaIds:*,"+arr[i]+",*");
               }
           }
           query.addFilterQuery(tradeAreaIds.toString());
        }
        if(hotelParam.getMaxPrice() != null && hotelParam.getMaxPrice() != 0){
            query.addFilterQuery("minPrice:[* TO "+hotelParam.getMaxPrice()+"]");
        }
        if(hotelParam.getMinPrice() != null && hotelParam.getMinPrice() != 0){
            query.addFilterQuery("minPrice:["+hotelParam.getMinPrice()+" TO *]");
        }
        if(hotelParam.getHotelLevel() != null && hotelParam.getHotelLevel() != 0){
            query.addFilterQuery("hotelLevel:"+hotelParam.getHotelLevel());
        }
        if(hotelParam.getFeatureIds() != null && hotelParam.getFeatureIds() != ""){
            String [] arr = hotelParam.getFeatureIds().split(",");
            StringBuffer featureIds = new StringBuffer();
            for(int i =0;i<arr.length;i++){
                if(i == 0){
                    featureIds.append("featureIds:*,"+arr[i]+",*");
                }else{
                    featureIds.append(" OR featureIds:*,"+arr[i]+",*");
                }
            }
            query.addFilterQuery(featureIds.toString());
        }
        if(hotelParam.getAscSort() != null &&  hotelParam.getAscSort() != ""){
            query.addSort(hotelParam.getAscSort(), SolrQuery.ORDER.asc);
        }
        if(hotelParam.getDescSort() != null && hotelParam.getDescSort() != ""){
            query.addSort(hotelParam.getDescSort(), SolrQuery.ORDER.desc);
        }
        query.setStart((pageBean.getCurPage() - 1) * pageBean.getPageSize());
        query.setRows(pageBean.getPageSize());
        QueryResponse response = client.query(query);
        pageBean.setTotal(new Long(response.getResults().getNumFound()).intValue());
        List<ItripHotelVO> list = client.query(query).getBeans(ItripHotelVO.class);
        pageBean.setRows(list);
    }
}
